# 获取html模块

from lxml import etree
import sys

def main(driver):
    title,picturelist = get_picture_url(driver)
    return title,picturelist




# 获取图片url 返回值 title（标题），picturelist（图片url列表）
def get_picture_url(driver):
    html = etree.HTML(driver.page_source)
    title = html.xpath('//title/text()')[0]
    nowpage = int(html.xpath('//input[@class="pic-toolbar-page"]/@value')[0])
    totlepage = int(html.xpath('//span[@class="pic-toolbar-total"]/text()')[0].split(' ')[-1])
    # print('title: ',title,'nowpage: ',nowpage,'totlepage: ',totlepage)

    js_afterpage = 'document.getElementsByClassName("pic-toolbar-right")[0].click()'
    js_beforepage = 'document.getElementsByClassName("pic-toolbar-left")[0].click()'
    # 向前翻倒第一页
    while True:
        if nowpage > 1:
            driver.execute_script(js_beforepage)
            nowpage = nowpage - 1
        else:
            # print('翻倒第{}页'.format(nowpage))
            break
    # 向后翻倒最后一页
    while True:
        if nowpage < totlepage:
            driver.execute_script(js_afterpage)
            nowpage = nowpage + 1
        else:
            # print('翻倒第{}页'.format(nowpage))
            break
    # 判断图片是否加载完成
    while True:
        html = etree.HTML(driver.page_source)
        pictureurl = html.xpath('//div[@data-show]/img/@src')
        # print(len(pictureurl))
        if len(pictureurl) == totlepage:
            break
    return title,pictureurl


